// 2025/11/11
// 两数相加

/**
 * Definition for singly-linked list.
 * struct ListNode {
 *     int val;
 *     ListNode *next;
 *     ListNode() : val(0), next(nullptr) {}
 *     ListNode(int x) : val(x), next(nullptr) {}
 *     ListNode(int x, ListNode *next) : val(x), next(next) {}
 * };
 */
class Solution {
public:
    ListNode* addTwoNumbers(ListNode* l1, ListNode* l2) {
        ListNode *cur1 = l1, *cur2 = l2, head, *cur = &head;
        int c = 0;
        while(c || cur1 || cur2)
        {
            int num1 = 0, num2 = 0;
            if(cur1) { num1 = cur1->val; cur1 = cur1->next; }
            if(cur2) { num2 = cur2->val; cur2 = cur2->next; }
            int sum = num1 + num2 + c;
            c = sum / 10;
            cur->next = new ListNode(sum % 10);
            cur = cur->next;
        }
        return head.next;
    }
};